Page({
  data: {
    workData: {},
    workExp: [],
    workIndex: '',
    modify: '',
    workBeginDefault: true,
    workEndDefault: true,
    workBeginDate: '2011-09-01',
    workEndDate: '2011-09-01',
    wrongTip: "内容有误"
  },
  onLoad: function (options) {
    var that = this;
    if (options.workData) {
      var workData = JSON.parse(options.workData);
      this.setData({
        modify: true,
        workIndex: options.workIndex,
        workData: workData,
        workBeginDate: workData.workBeginDate,
        workEndDate: workData.workEndDate
      })
      if (that.data.workData.workBeginDate) {
        that.setData({
          workBeginDefault: false
        })
      }
      if (that.data.workData.workEndDate) {
        that.setData({
          workEndDefault: false
        })
      }
    } else {
      this.setData({
        modify: false,
        workData: {},
        workBeginDate: '2011-09-01',
        workEndDate: '2011-09-01',
      })
    }
    wx.getStorage({
      key: 'workExp',
      success: function (res) {
        that.setData({
          workExp: res.data
        })
      }
    })
  },
  bindWorkBeginChange: function (e) {
    this.setData({
      workBeginDate: e.detail.value,
      workBeginDefault: false,
    })
  },
  bindWorkEndChange: function (e) {
    this.setData({
      workEndDate: e.detail.value,
      workEndDefault: false,
    })
  },
  formSubmit: function (e) {
    this.setData({
      workData: e.detail.value,
    })
    if (!this.data.workData.company) {
      wx.showModal({
        title: '提示',
        content: '请填写公司名称',
        showCancel:false,
        success:function(res){
        }
      })
    } else if (!this.data.workData.department) {
      wx.showModal({
        title: '提示',
        content: '请填写部门',
        showCancel: false,
        success: function (res) {
        }
      })
    }else if (!this.data.workData.job) {
      wx.showModal({
        title: '提示',
        content: '请填写职位',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.workBeginDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的入职时间',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.workEndDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的离职时间',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!this.data.workData.jobContent) {
      wx.showModal({
        title: '提示',
        content: '请填写工作内容',
        showCancel: false,
        success: function (res) {
        }
      })
    } else {
      if (this.data.modify == true) {
        this.data.workExp[this.data.workIndex] = this.data.workData
      } else {
        this.data.workExp.push(this.data.workData);
      }
      wx.setStorage({
        key: 'workExp',
        data: this.data.workExp,
        success: function (res) {
        }
      })
      wx.request({
        url: "",
        data: this.data.workExp,
        header: { 'Content-Type': 'application/json' },
        success: function (res) {
          console.log(res.data)
        }
      });
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        workExp: currPage.data.workExp,
        workShow: false,
        workModify: true,
      });
      wx.showModal({
        title: '提示',
        content: '信息更新成功，请继续完善简历',
        showCancel: false,
        success: function (res) {
          wx.navigateBack()
        }
      })
    }
  },
  reset:function(){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗',
      success: function (res) {
        if (res.confirm) {
           that.setData({
             workData:{},
             workBeginDefault: true,
             workEndDefault: true,
           })
           that.data.workExp.splice(that.data.workIndex,1);
           wx.setStorage({
             key: 'workExp',
             data: that.data.workExp,
             success: function (res) {
             }
           })
           var pages = getCurrentPages();
           var currPage = pages[pages.length - 1];
           var prevPage = pages[pages.length - 2];
           prevPage.setData({
             workExp: currPage.data.workExp,
           });
           console.log(that.data.workExp)
           console.log(that.data.workExp.length == 0)
           if (that.data.workExp.length==0) {
             prevPage.setData({
               workShow:true
             });
           }else{
             prevPage.setData({
               workShow: false
             });
           }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })  
  },
})